package cn.com.oceansoft.modules.satisfaction.controller;

import cn.com.oceansoft.application.constant.ConfigInfo;
import cn.com.oceansoft.application.common.util.HttpClientUtil;
import cn.com.oceansoft.framework.kits.ApiResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by liujie on 2016/10/6.
 * 用户满意度评价
 */
@Controller
@RequestMapping("satisfaction")
public class SatisfactionController {

    @Resource
    protected ConfigInfo configInfo;

    /**
     * 评价列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "case-list", method = GET)
    public ApiResult getCaseList(HttpServletRequest request) {
        String result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "comments/page",request.getParameterMap(),"UTF-8");
        return JSONObject.parseObject(result, ApiResult.class);
    }

    /**
     * 新增评价
     * @param satisfaction
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "do-satisfaction", method = POST)
    private ApiResult doSatisfaction(@RequestBody Map satisfaction) {
        String result = HttpClientUtil.doPostString(configInfo.getMsHostUrl() + "comments",satisfaction);
        ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }

    /**
     * 显示评价
     * @param guid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "show-satisfaction/{guid}", method = GET)
    private ApiResult getSatisfaction(@PathVariable("guid") String guid) {
        try {
            String result = HttpClientUtil.doGet(configInfo.getMsHostUrl() + "comments/"+guid);
            ApiResult apiResult = JSONObject.parseObject(result, ApiResult.class);
            return apiResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
